package com.heima.behavior.service.impl;

import com.heima.behavior.service.ApArticleBehaviorService;
import com.heima.behavior.service.ApBehaviorEntryService;
import com.heima.common.constants.user.UserRelationConstants;
import com.heima.common.exception.CustException;
import com.heima.model.behavior.dtos.ArticleBehaviorDTO;
import com.heima.model.behavior.pojos.ApBehaviorEntry;
import com.heima.model.behavior.pojos.ApCollection;
import com.heima.model.behavior.pojos.ApLikesBehavior;
import com.heima.model.behavior.pojos.ApUnlikesBehavior;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.AppThreadLocalUtils;
import com.heima.model.user.pojos.ApUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.annotation.UserConfigurations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @authar by xiecong
 * @Date 2022/3/11
 * @Description
 */
@Service
public class ApArticleBehaviorServiceImpl implements ApArticleBehaviorService {
    @Autowired
    ApBehaviorEntryService apBehaviorEntryService;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    StringRedisTemplate redisTemplate;

    /**
     * 加载文章详情 数据回显
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult loadArticleBehavior(ArticleBehaviorDTO dto) {
        //1. 判断用户是否登录
        //{"isfollow":true,"islike":true,"isunlike":false,"iscollection":true}
        //2. 未登录 直接将 4种行为 均返回false
        ApUser apUser = AppThreadLocalUtils.getUser();
        boolean isfollow = false;
        boolean islike = false;
        boolean isunlike = false;
        boolean iscollection = false;
        //3. 如果登录
        if (apUser != null) {
            //4. 查询行为实体
            ApBehaviorEntry apBehaviorEntry = apBehaviorEntryService
                    .findByUserIdOrEquipmentId(apUser.getId(), dto.getEquipmentId());
            if (apBehaviorEntry == null) {
                CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "实体行为数据不存在");
            }
            Query query = Query.query(Criteria
                    .where("entryId").is(apBehaviorEntry.getId())
                    .and("articleId").is(dto.getArticleId()));
            //5. 根据行为实体 文章ID查询 是否点赞
            ApLikesBehavior apLikesBehavior = mongoTemplate.findOne(query, ApLikesBehavior.class);
            if (apLikesBehavior != null) {
                islike = true;
            }
            //6. 根据行为实体 文章ID查询 是否不喜欢
            ApUnlikesBehavior unlikesBehavior = mongoTemplate.findOne(query, ApUnlikesBehavior.class);
            if (unlikesBehavior != null) {
                isunlike = true;
            }
            //7. 根据行为实体 文章ID查询 是否收藏
            ApCollection apCollection = mongoTemplate.findOne(query, ApCollection.class);
            if (apCollection != null) {
                iscollection = true;
            }
            //8. 根据登录用户id，去redis中查询是否关注该作者
            Double score = redisTemplate.opsForZSet().score(UserRelationConstants.FANS_LIST + apUser.getId()
                    , dto.getAuthorApUserId().toString());
            if (score != null) {
                isfollow = true;
            }
        }
        //9. 封装结果 返回
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("isfollow", isfollow);
        resultMap.put("islike", islike);
        resultMap.put("isunlike", isunlike);
        resultMap.put("iscollection", iscollection);
        return ResponseResult.okResult(resultMap);
    }
}
